"""
Case Type   : bugfix
Case Name   : 验证打开support_extended_features后可删除插件hstore
Create At   : 2025/6/25
Owner       : liu-tong-8848
Description :
    1、support_extended_features为默认值off时删除hstore
    2、设置support_extended_features为on，重启数据库
    3、删除hstore
    4、清理环境
Expect      :
    1、合理报错，EXTENSION is not yet supported
    2、成功
    3、成功
    4、成功
History     :
"""

import os
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class BugFix(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.user_node = Node('PrimaryDbUser')
        self.constant = Constant()
        sql_cmd = f'show support_extended_features;'
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.support_extended_features = sql_res.split("\n")[-2].strip()

    def test_dolphin(self):
        self.log.info(f'----{os.path.basename(__file__)} start----')
        text = '----step1:support_extended_features为默认值off时删除hstore；expect:合理报错----'
        self.log.info(text)
        msg = self.sh_primary.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG, f'support_extended_features=off')
        self.log.info(msg)
        self.assertTrue(msg, '执行失败' + text)
        result = self.sh_primary.restart_db_cluster()
        self.log.info(result)
        self.assertTrue(result)

        sql_cmd = f"show support_extended_features"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('off', sql_res, '执行失败' + text)

        sql_cmd = f"drop extension hstore;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertNotIn(self.constant.drop_extension_success, sql_res, '执行失败' + text)

        sql_cmd = f"\\dx+"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('hstore', sql_res, '执行失败' + text)

        text = '----step2:设置support_extended_features为on，重启数据库；expect:成功----'
        self.log.info(text)
        msg = self.sh_primary.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG, f'support_extended_features=on')
        self.log.info(msg)
        self.assertTrue(msg, '执行失败' + text)
        result = self.sh_primary.restart_db_cluster()
        self.log.info(result)
        self.assertTrue(result)

        sql_cmd = f"show support_extended_features"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('on', sql_res, '执行失败' + text)

        text = '----step3:删除hstore；expect:成功----'
        self.log.info(text)
        sql_cmd = 'drop extension hstore;'
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertNotIn('ERROR', sql_res, '执行失败' + text)
        self.assertIn(self.constant.drop_extension_success, sql_res, '执行失败' + text)

        sql_cmd = f"\\dx+"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertNotIn('hstore', sql_res, '执行失败' + text)

    def tearDown(self):
        text = '----step4:清理环境；expect:成功----'
        self.log.info(text)
        sql_cmd = f"create extension hstore;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)

        sql_cmd = f"\\dx+"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('hstore', sql_res, '执行失败' + text)

        msg = self.sh_primary.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG,
                                            f'support_extended_features={self.support_extended_features}')
        self.log.info(msg)
        self.assertTrue(msg, '执行失败' + text)
        result = self.sh_primary.restart_db_cluster()
        self.log.info(result)
        self.assertTrue(result)
        self.log.info(f'----{os.path.basename(__file__)} end-----')
